<?php
namespace Buu\Modules\HaohaoceManager\Lib;

use Buu\Modules\HaohaoceManager\Lib\K8sModel;
use Buu\Modules\HaohaoceManager\Lib\Util;
use Doraemon\service\DingTalkAlert;

class K8sView{
	const ALERT_ROBOT_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e3958d37-98c9-43ef-b09d-17f4cd0b5137';

	static private $status2cn = array(
		K8sModel::STATUS_PENDING => '<span class="label label-info">发布中</span>',
		K8sModel::STATUS_CONTAINERCREATING => '<span class="label label-info">容器创建中</span>',
		K8sModel::STATUS_CRASHLOOPBACKOFF => '<span class="label label-danger">容器加载错误</span>',
		K8sModel::STATUS_RUNNING => '<span class="label label-success">运行中</span>',
	);
	static function statusFormat($status){
		if(isset(self::$status2cn[$status])){ 
			return self::$status2cn[$status];
		}else{
			return $status;
		}
	}

	static function timeFormat($time){
		$duration = time()-$time;
		if($duration < 60){
			return $duration.'秒前';
		}elseif ($duration < 10%60) {
			return floor($duration/60).'分钟前';
		}else{
			return date('H:i:s' , $time);
		}
	}
	static function running_log($msg){
		// echo $msg."<br/>";
	}
	static function time_to_jump($url){

		echo '操作成功，请稍候...
		<script>window.setTimeout(
		function(){
			parent.location.href="'.$url.'"
		} , 2);</script>';
	}
	static function display_time($second){
		if($second > 3600){
			return floor($second/3600).'小时';
		}else if($second > 60){
			return floor($second/60).'分钟';
		}else{
			return $second.'秒';
		}
	}
	static function alert_pod_is_builded($pod_info){
		$msg = "{name} ,你的测试环境已经完成部署！\n总用时：{time}";
		$time = time()-$pod_info['create_time'];

		$msg = str_replace('{name}' , $pod_info['admin_name'] , $msg);
		$msg = str_replace('{time}' , self::display_time($time) , $msg);

		// DingTalkAlert::sendTextMsg(DingTalkAlert::HHC_TEST_ENVIRONMENT_NOTICE, $msg, false, [$pod_info['admin_email']]);
	}
	static function alert_pod_will_be_expire($pod_info){
		$msg = '{name} ,你的测试环境将在'.$pod_info['expire'].'过期';
		$msg = str_replace('{name}' , $pod_info['admin_name'] , $msg , [$name_at]);
		
		// DingTalkAlert::sendTextMsg(DingTalkAlert::HHC_TEST_ENVIRONMENT_NOTICE, $msg, false, [$pod_info['admin_email']]);
	}
	static function alert_pod_is_expired($pod_info){
		$msg = '{name} ,你的测试环境已经过期收回';
		$msg = str_replace('{name}' , $pod_info['admin_name'] , $msg);

		// DingTalkAlert::sendTextMsg(DingTalkAlert::HHC_TEST_ENVIRONMENT_NOTICE, $msg, false, [$pod_info['admin_email']]);
	}
}